<template>
  <div>
    <page-list :x-uri="uri" :custom-components="custom" :doactions="doactions">
      <!-- 自定义tab -->
      <template v-slot:page-tabs>
        <tabs />
      </template>
      <!-- 页面操作按钮 -->
      <template v-slot:page-button>
        <el-button v-auth="'course.live.small.create'" size="mini" type="primary" @click="create">新建直播</el-button>
      </template>
      <!-- 多选操作 -->
      <template v-slot:table-button="scope">
        <el-button v-auth="'course.live.small.edit'" size="mini" @click="listing(scope)">上架</el-button>
        <el-button v-auth="'course.live.small.edit'" size="mini" @click="disable(scope)">下架</el-button>
        <el-button v-auth="'course.live.small.delete'" size="mini" @click="deletes(scope)">删除</el-button>
      </template>
    </page-list>
    <sort-dialog :value.sync="sortValue" :visible.sync="visible" :disable-submit="disableSubmit" @submit="onSubmit" />
  </div>
</template>
<script>
import PageList from '@public/template/page_list.vue';
import Status from './components/status';
import UpdateStatus from './components/update_status';
import Titile from '../video/components/title.vue';
import IsRecommend from './components/is_recommend.vue';
import CreditRedeem from './components/credit_redeem.vue';
import Tabs from '../live/components/tabs.vue';
import SortDialog from '@/components/update-sort-dialog';
import CategoryTitle from '@/components/category-title'

export default {
  components: {
    PageList,
    Tabs,
    SortDialog
  },
  inject: ['reload'],
  data() {
    return {
      uri: 'course/admin/live/small/list',
      custom: {
        status: Status,
        update_status: UpdateStatus,
        title: Titile,
        is_recommend: IsRecommend,
        credit_redeem: CreditRedeem,
        cates: CategoryTitle
      },
      doactions: [
        {
          title: '课时管理',
          meta: { permission: 'course.live.small.section' },
          callback: this.section
        },
        { title: '学员', meta: { permission: 'course.live.small.student' }, callback: this.student },
        {
          title: '课件',
          meta: { permission: 'course.live.small.courseware' },
          callback: this.courseware
        },
        { title: '排序', meta: { permission: 'course.live.small.order' }, callback: this.sortPopover },
        {
          title: '编辑',
          meta: { permission: 'course.live.small.edit' },
          callback: this.edit
        },
        { title: '代理', meta: { permission: 'course.live.small.agent' }, callback: this.agent },
        { title: '考试', meta: { permission: 'course.live.small.exam' }, callback: this.exam },
        { title: '评论', meta: { permission: 'course.live.small.comment' }, callback: this.comment },
        // { title: '笔记', meta: { permission: 'course.live.small.note' }, callback: this.note },
        { title: '收藏', meta: { permission: 'course.live.small.collect' }, callback: this.collection },
        { title: '加购', meta: { permission: 'course.live.small.present' }, callback: this.cart },
        { title: '复制', meta: { permission: 'course.live.small.copy' }, callback: this.copyCourse },
        { title: '删除', meta: { permission: 'course.live.small.delete' }, callback: this.delete }
      ],
      itemId: 0,
      visible: false,
      disableSubmit: false,
      sortValue: 1 // 排序值
    };
  },
  methods: {
    create: function() {
      this.$router.push({ path: '/course/live/small/create' });
    },
    // 考试
    exam: function({ row }) {
      this.$router.push({ path: '/course/exam/' + row.id + '/list', query: { type: 'live_small' }});
    },
    // 评论
    comment: function({ row }) {
      this.$router.push({ path: '/course/comment/' + row.id + '/list' });
    },
    // 笔记
    note: function({ row }) {
      this.$router.push({ path: '/course/note/' + row.id + '/list' });
    },
    // 收藏
    collection: function({ row }) {
      this.$router.push({ path: '/course/collection/' + row.id + '/list' });
    },
    // 加购
    cart: function({ row }) {
      this.$router.push({ path: '/course/cart/' + row.id + '/list' });
    },
    // 复制课程
    copyCourse: function({ row }) {
      this.$box
        .confirm('确认复制课程信息吗')
        .then(() => {
          this.$http.post(
            '/course/admin/' + row.id + '/copy',
            {},
            response => {
              this.$message.success(response.msg, _ => {
                this.reload();
              });
            },
            error => {
              this.$message.error(error.msg);
            }
          );
        })
        .catch(() => {});
    },
    section: function({ row }) {
      this.$router.push({ path: '/course/live/small/' + row.id + '/section' });
    },
    edit: function({ row }) {
      this.$router.push({ path: '/course/live/small/' + row.id + '/edit' });
    },
    // 代理
    agent: function({ row }) {
      // 错误提示
      if (row.mhm_id > 1 || row.status == 0 || row.solo == 0) {
        this.$message.warning('该课程不满足代理条件，仅平台课程才能代理');
        return false;
      }
      this.$router.push({ path: '/course/agent/' + row.id + '/info' });
    },
    delete: function({ row }) {
      this.$box
        .confirm('确认删除吗')
        .then(() => {
          this.$http.delete(
            '/course/admin/live/small/' + row.id,
            {},
            response => {
              this.$message.success(response.msg, _ => {
                this.reload();
              });
            },
            error => {
              this.$message.error(error.msg);
            }
          );
        })
        .catch(() => {});
    },
    student: function({ row }) {
      this.$router.push({ path: '/course/live/small/' + row.id + '/student' });
    },
    // 课件
    courseware: function({ row }) {
      this.$router.push({ path: '/course/courseware/' + row.id + '/list' });
    },
    // 批量上架课程
    listing: function({ rows }) {
      var ids = rows.map(row => row.id);
      if (ids.length === 0) {
        this.$message.warning('请选择要上架的课程');
        return false;
      }
      this.$box
        .confirm('确认上架吗')
        .then(() => {
          this.$http.put(
            '/course/admin/live/small/listing',
            {
              ids: ids
            },
            response => {
              this.$message.success(response.msg, _ => {
                this.reload();
              });
            },
            error => {
              this.$message.error(error.msg);
            }
          );
        })
        .catch(() => {});
    },
    // 批量禁用课程
    disable: function({ rows }) {
      var ids = rows.map(row => row.id);
      if (ids.length === 0) {
        this.$message.warning('请选择要禁用的课程');
        return false;
      }
      this.$box
        .confirm('确认禁用吗')
        .then(() => {
          this.$http.put(
            '/course/admin/live/small/disables',
            {
              ids: ids
            },
            response => {
              this.$message.success(response.msg, _ => {
                this.reload();
              });
            },
            error => {
              this.$message.error(error.msg);
            }
          );
        })
        .catch(() => {});
    },
    // 批量删除课程
    deletes: function({ rows }) {
      var ids = rows.map(row => row.id);
      if (ids.length === 0) {
        this.$message.warning('请选择要删除的课程');
        return false;
      }
      ids = ids.join(',');
      this.$box
        .confirm('确认删除吗')
        .then(() => {
          this.$http.delete(
            '/course/admin/live/small/deletes/' + ids,
            {},
            response => {
              this.$message.success(response.msg, _ => {
                this.reload();
              });
            },
            error => {
              this.$message.error(error.msg);
            }
          );
        })
        .catch(() => {});
    },
    // 排序弹出层
    sortPopover({ row }) {
      this.visible = true;
      this.itemId = row.id;
      this.sortValue = row.best_sort;
    },
    // 保存
    onSubmit() {
      this.disableSubmit = true;
      this.$http.put(
        '/course/admin/live/small/' + this.itemId + '/edit/best_sort',
        {
          best_sort: this.sortValue
        },
        ({ msg }) => {
          this.$message.success(msg);
          this.disableSubmit = false;
          this.visible = false;
          this.reload();
        },
        ({ msg }) => {
          this.$message.error(msg);
          this.disableSubmit = false;
        },
        false
      );
    }
  }
};
</script>
